package com.sk.cardeal.act.home.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sk.cardeal.R;
import com.sk.cardeal.act.home.activity.StateFixingActivity;
import com.sk.cardeal.act.home.activity.WaitLeaderSureActivity;
import com.sk.cardeal.adapter.WorkFormAdapter;
import com.sk.cardeal.base.BaseFragment;
import com.sk.cardeal.callback.HandleDataCallBack;
import com.sk.cardeal.entity.WorkForm;
import com.sk.cardeal.entity.WorkFormList;
import com.sk.cardeal.utils.AppApi;
import com.sk.cardeal.view.MultiStateView;
import com.sk.cardeal.view.SimpleMultiStateView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Created by sk on 2017/12/15.
 */

public class FixingFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener, HandleDataCallBack, CompoundButton.OnCheckedChangeListener {
    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;
    @BindView(R.id.mPtrFrameLayout)
    PtrClassicFrameLayout mPtrFrameLayout;
    @BindView(R.id.fixing_box)
    CheckBox fixingBox;
    @BindView(R.id.wait_check)
    CheckBox waitCheck;
    @BindView(R.id.SimpleMultiStateView)
    SimpleMultiStateView simpleMultiStateView;
    @BindView(R.id.wait_sure)
    CheckBox waitSure;

    private int page = 0;
    private List<WorkForm> allDataList = new ArrayList<>();
    WorkFormAdapter adapter;
    private boolean isLoadMore = false;
    private boolean isRefresh = false;
    private String state = "";

    private List<CheckBox> checkBoxList = new ArrayList<>();

    private List<CheckBox> selectBox = new ArrayList<>();

    @Override
    protected int getContentView() {
        return R.layout.layout_fixing_fragment;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initStateView();

        mPtrFrameLayout.disableWhenHorizontalMove(true);

        fixingBox.setTag("2");
        waitCheck.setTag("3");
        waitSure.setTag("11");

        checkBoxList.add(fixingBox);
        checkBoxList.add(waitCheck);
        checkBoxList.add(waitSure);

        for (CheckBox checkBox : checkBoxList) {
            checkBox.setOnCheckedChangeListener(this);
        }

        mPtrFrameLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, dataRyc, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                isRefresh = true;
                doRefresh();
            }
        });

        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new WorkFormAdapter(R.layout.item_work_form, allDataList);
        dataRyc.setAdapter(adapter);

        View emptyView = LayoutInflater.from(mContext).inflate(R.layout.empty_view, null);
        TextView textView = emptyView.findViewById(R.id.content);
        textView.setText("暂无该类工单哦！");
        adapter.setEmptyView(emptyView);

        adapter.setOnItemClickListener(this);
        adapter.setOnLoadMoreListener(this, dataRyc);
        AppApi.getWorkFormList(state, 2, page, this, 0, getActivity());

    }


    public void doRefresh() {
        page = 0;
        AppApi.getWorkFormList(state, 2, page, this, 0, getActivity());
    }

    @Override
    public void handlerSuccessData(String result, int tag) {

        switch (tag) {
            case 0:
                WorkFormList workFormList = JSON.parseObject(result, WorkFormList.class);
                List<WorkForm> dataBeanList = workFormList.getResultList();
                showSuccess();
                if (isRefresh) {
                    isRefresh = false;
                    mPtrFrameLayout.refreshComplete();
                }

                if (page == 0) {
                    allDataList.clear();
                }

                allDataList.addAll(dataBeanList);
                if (allDataList.size() < 10) {
                    adapter.setEnableLoadMore(false);
                } else {
                    adapter.setEnableLoadMore(true);
                }
                if (isLoadMore) {
                    adapter.loadMoreComplete();
                    if (dataBeanList.size() == 0) {
                        adapter.loadMoreEnd();
                    }
                    isLoadMore = false;
                }
                adapter.notifyDataSetChanged();
                break;
        }
    }


    @Override
    public void onLoadMoreRequested() {
        isLoadMore = true;
        page++;
        AppApi.getWorkFormList(state, 2, page, this, 0, getActivity());
    }

    @Override
    public void handlerFailData(String failString, int tag) {

    }

    @Override
    public void handleErrorData(String error, int tag) {

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        int state = allDataList.get(position).getState();
        Intent intent = new Intent();
        switch (state) {
            case 2:
                intent.setClass(mContext, StateFixingActivity.class);
                intent.putExtra("id", allDataList.get(position).getId());
                getActivity().startActivityForResult(intent, 10);
                break;
            case 3:
                intent.setClass(mContext, WaitLeaderSureActivity.class);
                intent.putExtra("id", allDataList.get(position).getId());
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        if (isChecked) {
            switch (buttonView.getId()) {
                case R.id.fixing_box:
                    selectBox.add(fixingBox);
                    break;
                case R.id.wait_check:
                    selectBox.add(waitCheck);
                    break;
                case R.id.wait_sure:
                    selectBox.add(waitSure);
                    break;
            }

        } else {
            switch (buttonView.getId()) {
                case R.id.fixing_box:
                    selectBox.remove(fixingBox);
                    break;
                case R.id.wait_check:
                    selectBox.remove(waitCheck);
                    break;
                case R.id.wait_sure:
                    selectBox.remove(waitSure);
                    break;
            }
        }
        getStateData();
    }

    private void getStateData() {
        state = "";
        StringBuilder sb = new StringBuilder();
        if (selectBox.size() == 0) {
            state = "";
        } else {
            for (int i = 0; i < selectBox.size(); i++) {
                if (i == selectBox.size() - 1) {
                    String s = (String) selectBox.get(i).getTag();
                    sb.append(s);
                } else {
                    String s = (String) selectBox.get(i).getTag();
                    sb.append(s);
                    sb.append(",");
                }
            }
            state = sb.toString();
        }
        page = 0;
        showLoading();
        AppApi.getWorkFormList(state, 2, page, this, 0, getActivity());
    }

    private void initStateView() {
        if (simpleMultiStateView == null) return;
        simpleMultiStateView.setEmptyResource(R.layout.view_empty)
                .setRetryResource(R.layout.view_retry)
                .setLoadingResource(R.layout.view_loading)
                .setNoNetResource(R.layout.view_nonet)
                .build()
                .setonReLoadlistener(new MultiStateView.onReLoadlistener() {
                    @Override
                    public void onReload() {
                    }
                });
    }

    public void showLoading() {
        if (simpleMultiStateView != null) {
            simpleMultiStateView.showLoadingView();
        }
    }

    public void showSuccess() {
        if (simpleMultiStateView != null) {
            simpleMultiStateView.showContent();
        }
    }

    public void showFaild() {
        if (simpleMultiStateView != null) {
            simpleMultiStateView.showErrorView();
        }
    }

    public void showNoNet() {
        if (simpleMultiStateView != null) {
            simpleMultiStateView.showNoNetView();
        }
    }

}
